package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    
    @Autowired
    private IPermissionService permissionService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    @RequiresPermissions(value = {"permission:list","权限列表"},logical = Logical.OR)
    public String list(Model model , @ModelAttribute("qo") QueryObject qo){
        //调用service的方法的到结果集
        PageInfo result = permissionService.query(qo);
        //共享结果集：在页面遍历显示
        model.addAttribute("result", result);
        return "permission/list"; //返回视图名称：加上前缀和后缀的jsp
    }


    @RequestMapping("/delete")
    @RequiresPermissions(value = {"permission:delete","权限删除"},logical = Logical.OR)
    public String delete(Long id){
        if (id != null) {
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do";  // http://localhost/permission/list.do
    }

    @RequestMapping("/reload")
    @RequiresPermissions(value = {"permission:reload","权限加载"},logical = Logical.OR)
    @ResponseBody//把返回的对象转成JSON格式的数据响应给前端
    public JsonResult reload(){
        try {
            permissionService.reload();
            return  new JsonResult(true,"加载成功");
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"加载失败");
        }
    }
}
